package Game.scene;

import static org.lwjgl.opengl.GL11.GL_AMBIENT;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST;
import static org.lwjgl.opengl.GL11.GL_DIFFUSE;
import static org.lwjgl.opengl.GL11.GL_LIGHT0;
import static org.lwjgl.opengl.GL11.GL_LIGHTING;
import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
import static org.lwjgl.opengl.GL11.GL_POSITION;
import static org.lwjgl.opengl.GL11.GL_PROJECTION;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glEnable;
import static org.lwjgl.opengl.GL11.glLight;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.opengl.GL11.glPopMatrix;
import static org.lwjgl.opengl.GL11.glPushMatrix;
import static org.lwjgl.opengl.GL11.glRotatef;
import static org.lwjgl.opengl.GL11.glScalef;
import static org.lwjgl.opengl.GL11.glTranslatef;
import static org.lwjgl.util.glu.GLU.gluLookAt;
import static org.lwjgl.util.glu.GLU.gluPerspective;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.LinkedList;

import org.lwjgl.input.Keyboard;

import Game.Cube;
import Game.GameObject;
import Game.screen.TelaEntrada;
import Game.window.GameMain;



public class StartScreen extends IScene{

	private final static double FOVY = 45.0;
	private final static double NEAR = 0.1;
	private final static double FAR = 1000.0;
	private float angTY;
	float worldX, worldY, worldZ;
	private float X;
	private float Y;
	private int Z;
	private long keyboardCooldown;
	
	private static LinkedList<GameObject> childList = new LinkedList<GameObject>();
	
	@Override
	public void init() {
		glEnable(GL_DEPTH_TEST);
		glEnable(GL_LIGHTING);

		int width = GameMain.instance.display_parent.getWidth();
		int height = GameMain.instance.display_parent.getHeight();

		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		float aspect = (float) width / (float) height;
		gluPerspective((float) FOVY, (float) aspect, (float) NEAR, (float) FAR);
		glMatrixMode(GL_MODELVIEW);
		
		
		
		TelaEntrada cube = new TelaEntrada(0,0,0,"LogoUNIVALI.png");
		cube.w = 3.4f;
		cube.h = 2.5f;
		cube.d = 2;
		
		addChild(cube);
	}
	
	public static void addChild(GameObject gameObject) {
		childList.add(gameObject);
	}

	@Override
	public void reshape(int x, int y, int width, int height) {
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		float aspect = (float) width / (float) height;
		gluPerspective((float) FOVY, (float) aspect, (float) NEAR, (float) FAR);
		glMatrixMode(GL_MODELVIEW);
	}
	
	@Override
	public void update(long diftime) {
		
		if (Keyboard.isKeyDown(Keyboard.KEY_UP)){
			if(System.currentTimeMillis() - keyboardCooldown > 1000){
				Z+= 1 * diftime /1000;
				keyboardCooldown = System.currentTimeMillis();
			}
				
		}
		
		if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)){
			if(System.currentTimeMillis() - keyboardCooldown > 1000){
				Z-= 1 * diftime /1000;
				keyboardCooldown = System.currentTimeMillis();
			}
				
		}
	}

	@Override
	public void render() {
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		glLoadIdentity();
		
		float[] lightDiffuse = { 1.0f, 1.0f, 1.0f, 1.0f };
		float[] lightAmbient = { 1.0f, 1.0f, 1.0f, 1.0f };
		float[] lightPosition = { 1.0f, worldX, worldY, worldZ };

		ByteBuffer temp = ByteBuffer.allocateDirect(16);
		temp.order(ByteOrder.nativeOrder());
		glLight(GL_LIGHT0, GL_AMBIENT,
				(FloatBuffer) temp.asFloatBuffer().put(lightAmbient).flip());
		glLight(GL_LIGHT0, GL_DIFFUSE,
				(FloatBuffer) temp.asFloatBuffer().put(lightDiffuse).flip());
		glLight(GL_LIGHT0, GL_POSITION,
				(FloatBuffer) temp.asFloatBuffer().put(lightPosition).flip());

		glEnable(GL_LIGHT0);

		gluLookAt( X, Y, Z-4.f, 0, 0, 0,
				0.0f, 1.0f, 0.0f);

		glPushMatrix();
		{
			glEnable(GL_TEXTURE_2D);
			recursiveDraw(childList, null);
		}
		glPopMatrix();
	}
	
	private void recursiveDraw(LinkedList<GameObject> childList,
			GameObject parent) {
		for (int i = 0; i < childList.size(); i++) {
			GameObject obj = childList.get(i);

			if (!obj.alive) {
				childList.remove(obj);
			}

			glPushMatrix();

			if (parent != null) {

				glTranslatef(parent.x + obj.x, parent.y + obj.y, parent.z
						+ obj.z);

				glRotatef(obj.rotateX + parent.rotateX, 1, 0, 0);
				glRotatef(obj.rotateY + parent.rotateY, 0, 1, 0);
				glRotatef(obj.rotateZ + parent.rotateZ, 0, 0, 1);

			} else {

				glTranslatef(obj.x, obj.y, obj.z);

				glRotatef(obj.rotateX, 1, 0, 0);
				glRotatef(obj.rotateY, 0, 1, 0);
				glRotatef(obj.rotateZ, 0, 0, 1);
			}

			glScalef(obj.scaleX, obj.scaleY, obj.scaleZ);
			obj.Render();

			glPopMatrix();
			recursiveDraw(obj.childList, obj);
		}
	}

}
